using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;
using UnityEngine;

public class Humanoid : Mortal
{
	public ParticleSystem headshotParticle;

	public ParticleSystem damageParticle;

	public PlayerMecanimWrapper mecanimWrapper;

	public WeaponEffectWatcher weaponEffectWatcher;

	public MobNavigationController mobNavigationController;

	public EquipmentManager equipmentManager;

	public bool canMove = true;

	public bool canShoot = true;

	public bool inCar;

	public bool inHelic;

	public float cameraAngle;

	public Collider mainCollider;

	public Material mainMaterial;

	protected GameObject prefabPlayer;

	protected string equipedSkinID;

	protected bool _isAnimDamagePlaying;

	protected PlayerSounds soundsManager;

	[HideInInspector]
	public bool IsInvisible;

	[HideInInspector]
	public NJGMapItem iconMiniMap;

	[HideInInspector]
	public Color colorDamage = Color.white;

	private List<Material> armorMaterials = new List<Material>();

	public Material deadMaterial;

	public Material normalMaterial;

	public Texture deadTexture;

	[HideInInspector]
	public float _invisibleValue;

	protected Tweener _invisibleTween;

	private SkinnedMeshRenderer meshRenderer;

	private SkinnedMeshRenderer meshRendererFPS;

	private IEnumerator flickerWhileGettingUp;

	private int indexMiniMap;

	protected float timeForRespawn = 2f;

	public override void Awake()
	{
		base.Awake();
		equipmentManager = GetComponent<EquipmentManager>();
		weaponEffectWatcher = GetComponent<WeaponEffectWatcher>();
		mecanimWrapper = GetComponent<PlayerMecanimWrapper>();
		iconMiniMap = GetComponent<NJGMapItem>();
		soundsManager = GetComponentInChildren<PlayerSounds>();
		if (base.isMine && equipmentManager != null)
		{
			equipmentManager.EquipAllItems();
		}
		if (prefabPlayer == null)
		{
			prefabPlayer = base.transform.Find("player_mecanim").gameObject;
		}
	}

	private new void OnEnable()
	{
		iconMiniMap.enabled = true;
	}

	public virtual void ShowIndicatorCriticalDamague(Mortal target)
	{
		if (target != null)
		{
			target.ShowCriticalDamage();
		}
	}

	public virtual void ShowHeadshot()
	{
		ShowCriticalDamage();
	}

	public virtual void SetImmun(EffectType[] types)
	{
		if ((bool)weaponEffectWatcher)
		{
			foreach (EffectType immun in types)
			{
				SetImmun(immun);
			}
		}
	}

	public override void GetDamage(int damage, int damageType, int iKillerId, Vector3 positionKiller, Vector3? hitPoint = null)
	{
		base.GetDamage(damage, damageType, iKillerId, positionKiller);
	}

	public override void Kill(int id)
	{
		base.Kill(id);
		if (soundsManager != null)
		{
			soundsManager.Death();
		}
		SetIconDieMiniMap(true);
		mecanimWrapper.SwitchUpperLayer(false);
		mecanimWrapper.HoldShoot(false);
		DeadTexture(true);
	}

	public override void Respawn(Vector3 position)
	{
		base.Respawn(position);
		SetIconDieMiniMap(false);
		DeadTexture(false);
	}

	public virtual void SetImmun(EffectType type)
	{
		if (weaponEffectWatcher == null)
		{
			weaponEffectWatcher = GetComponent<WeaponEffectWatcher>();
		}
		weaponEffectWatcher.SetImmun(type);
	}

	public override void ShowCriticalDamage()
	{
		base.ShowCriticalDamage();
		if (headshotParticle != null && GameController.instance.playerScript != this && !isDead)
		{
			headshotParticle.Emit(1);
		}
	}

	public void DeadTexture(bool activate)
	{
		if (mainMaterial == null)
		{
			return;
		}
		for (int i = 0; i < armorMaterials.Count; i++)
		{
			armorMaterials[i].SetInt("_Redness", 1);
			armorMaterials[i].SetColor("_Color", Color.red);
			if (activate)
			{
				mainMaterial.SetColor("_Color", Color.white);
				mainMaterial.SetFloat("_Alpha", 0.2f);
				mainMaterial.SetInt("_Redness", 1);
			}
			else
			{
				mainMaterial.SetFloat("_Alpha", 1f);
				mainMaterial.SetInt("_Redness", 0);
			}
		}
	}

	protected void CreateMaterial()
	{
		Material material = null;
		Renderer[] componentsInChildren = prefabPlayer.GetComponentsInChildren<Renderer>(true);
		Renderer[] array = componentsInChildren;
		foreach (Renderer renderer in array)
		{
			if (renderer.gameObject.tag != "Weapon" && renderer.gameObject.layer != 26)
			{
				if (material == null)
				{
					material = new Material(renderer.material);
				}
				renderer.material = material;
			}
		}
		mainMaterial = material;
	}

	public override void ShowNormalDamage()
	{
		base.ShowNormalDamage();
		if (mainMaterial != null && !_isAnimDamagePlaying && !isDead)
		{
			Renderer[] componentsInChildren = GetComponentsInChildren<Renderer>();
			foreach (Renderer renderer in componentsInChildren)
			{
				if (renderer.gameObject.tag != "Weapon" && renderer.gameObject.layer != 26)
				{
					armorMaterials.Add(renderer.material);
				}
			}
			for (int j = 0; j < armorMaterials.Count; j++)
			{
				armorMaterials[j].SetInt("_Redness", 1);
				armorMaterials[j].SetColor("_Color", Color.red);
			}
			_isAnimDamagePlaying = true;
			CancelInvoke("NormalColor");
			Invoke("NormalColor", 0.07f);
		}
		if ((bool)damageParticle && GameController.instance.playerScript != this && !isDead)
		{
			damageParticle.Emit(1);
		}
	}

	public void NormalColor()
	{
		for (int i = 0; i < armorMaterials.Count; i++)
		{
			armorMaterials[i].SetInt("_Redness", 0);
		}
		armorMaterials.Clear();
		Invoke("CanRunAnimDamage", 0.07f);
	}

	protected void AppearFromInvisible()
	{
		if (_invisibleTween != null)
		{
			HOTween.Kill(_invisibleTween);
			_invisibleTween = null;
		}
		_invisibleValue = 0f;
		mainMaterial.SetFloat("_Alpha", _invisibleValue);
		_invisibleTween = HOTween.To(this, 1.5f, new TweenParms().Prop("_invisibleValue", 1f).Ease(EaseType.EaseInExpo).OnUpdate(delegate
		{
			mainMaterial.SetFloat("_Alpha", _invisibleValue);
		}, new object[0])
			.OnComplete(delegate
			{
				_invisibleTween = null;
			}, new object[0]));
	}

	public void CanRunAnimDamage()
	{
		_isAnimDamagePlaying = false;
	}

	public bool CanBeAttacked()
	{
		bool flag = inCar || inHelic;
		return (!IsInvisible || flag) && MobSpawnController.instance.canSpawnAggressiveMobs;
	}

	public void switchTextureOn(GameObject player, Texture newTex)
	{
	}

	public override void AddWeaponEffect(int effectType, int damageType, float effectDuration, float effectPower, int killerID)
	{
		base.AddWeaponEffect(effectType, damageType, effectDuration, effectPower, killerID);
		if ((!TDMController.isTeamGameType || !(this is PlayerBehavior) || (this as PlayerBehavior).localTeam != TDMController.myTeam) && !isDead && !inCar && !inHelic && (bool)weaponEffectWatcher)
		{
			AddWeaponEffectRPC(effectType, damageType, effectDuration, effectPower, killerID);
		}
	}

	[PunRPC]
	public void AddWeaponEffectRPC(int effectType, int damageType, float effectDuration, float effectPower, int killerID)
	{
		WeaponEffectSettings weaponEffectSettings = new WeaponEffectSettings();
		weaponEffectSettings.effectType = (EffectType)effectType;
		weaponEffectSettings.damageType = (DamageType)damageType;
		weaponEffectSettings.effectDuration = effectDuration;
		weaponEffectSettings.effectPower = effectPower;
		weaponEffectSettings.killerID = killerID;
		bool flag = weaponEffectWatcher.AddNewEffect(weaponEffectSettings);
		if (!settings.offlineMode && flag)
		{
			base.photonView.RPC("AddWeaponEffectRPC", PhotonTargets.Others, effectType, damageType, effectDuration, effectPower, killerID);
		}
	}

	protected void ChangePlayerAnimationSet(BaseWeapon weapon)
	{
		float timeBetweenShots = weapon.TimeBetweenShots;
		float reloadTime = weapon.ReloadTime;
		if (weapon.weaponMecanimWrapper != null)
		{
			weapon.weaponMecanimWrapper.Init(mecanimWrapper, reloadTime, timeBetweenShots, true);
			weapon.SwitchFPSCameraMode(CameraModeControl.instance.mainCameraType == settings.MainCameraType.FPSCamera && base.isMine && !weapon.isMobWeapon);
		}
		AnimatorOverrideController animatorOverrideController = GetAnimatorOverrideController(weapon);
		if ((bool)animatorOverrideController)
		{
			mecanimWrapper.SetWeapon(animatorOverrideController, reloadTime, timeBetweenShots, weapon.mechanicType == MechanickType.Charging);
		}
	}

	protected void SetSkin(string id)
	{
		equipedSkinID = id;
		if (prefabPlayer == null)
		{
			prefabPlayer = base.transform.Find("player_mecanim").gameObject;
		}
		if (meshRenderer == null)
		{
			SkinnedMeshRenderer[] componentsInChildren = base.transform.GetComponentsInChildren<SkinnedMeshRenderer>(true);
			if (componentsInChildren[0] != null)
			{
				meshRenderer = componentsInChildren[0];
			}
			if (componentsInChildren[1] != null)
			{
				meshRendererFPS = componentsInChildren[1];
			}
		}
		try
		{
			meshRenderer.material.mainTexture = MarketController.Instance.GetSkinTexture(id);
			meshRendererFPS.material.mainTexture = meshRenderer.material.mainTexture;
		}
		catch
		{
		}
	}

	[PunRPC]
	public void Invisible(bool activate)
	{
	}

	protected AnimatorOverrideController GetAnimatorOverrideController(BaseWeapon weapon)
	{
		AnimatorOverrideController result = weapon.weaponMecanimWrapper.overrideController;
		if (!weapon.isMobWeapon && CameraModeControl.instance.mainCameraType == settings.MainCameraType.FPSCamera && (settings.offlineMode || base.isMine) && weapon.weaponMecanimWrapper.overrideControllerFPS != null)
		{
			result = weapon.weaponMecanimWrapper.overrideControllerFPS;
		}
		else if (MarketController.Instance.IsFemaleSkin(equipedSkinID))
		{
			result = weapon.weaponMecanimWrapper.overrideControllerFemale;
		}
		return result;
	}

	protected void StartFlickerWhileGettingUp(Material material)
	{
		if (flickerWhileGettingUp != null)
		{
			StoptFlickerWhileGettingUp(material);
		}
		flickerWhileGettingUp = FlickerWhileGettingUp(material);
		StartCoroutine(flickerWhileGettingUp);
	}

	protected void StoptFlickerWhileGettingUp(Material material)
	{
		if (flickerWhileGettingUp != null)
		{
			StopCoroutine(flickerWhileGettingUp);
			flickerWhileGettingUp = null;
		}
		if (material != null)
		{
			material.SetFloat("_Alpha", 1f);
		}
	}

	private IEnumerator FlickerWhileGettingUp(Material material)
	{
		yield return new WaitForSeconds(0.2f);
		bool state = false;
		float timeChangeAlpha = 0.2f;
		float time = 0f;
		while (true)
		{
			time += Time.deltaTime;
			if (time > timeChangeAlpha)
			{
				if (material != null)
				{
					material.SetFloat("_Alpha", (!state) ? (0.4f + 0.6f * time / timeChangeAlpha) : (1f - 0.6f * time / timeChangeAlpha));
				}
				state = !state;
				time = 0f;
			}
			yield return null;
		}
	}

	public virtual void SetIconDieMiniMap(bool val)
	{
		if (!(PlayerBehavior.MyPlayer != this))
		{
			return;
		}
		if (val)
		{
			indexMiniMap = iconMiniMap.type;
			iconMiniMap.dontShowIcon = true;
			iconMiniMap.enabled = false;
			return;
		}
		iconMiniMap.enabled = true;
		if (this is PlayerBehavior && ((PlayerBehavior)this).imLeader)
		{
			iconMiniMap.SetType("Player_Leader");
		}
		else if (indexMiniMap != 0)
		{
			iconMiniMap.type = indexMiniMap;
		}
	}

	public override void ShowKill(DamageType damage)
	{
		PlayerDeadAnimation playerDeadAnimation;
		switch (damage)
		{
		case DamageType.Electric:
			playerDeadAnimation = PlayerDeadAnimation.Normal;
			break;
		case DamageType.Energy:
			playerDeadAnimation = PlayerDeadAnimation.Normal;
			break;
		case DamageType.Explosive:
			playerDeadAnimation = PlayerDeadAnimation.Normal;
			break;
		case DamageType.Fire:
			playerDeadAnimation = PlayerDeadAnimation.Fire;
			break;
		case DamageType.Freaze:
			playerDeadAnimation = PlayerDeadAnimation.Freezing;
			break;
		case DamageType.Kick:
			playerDeadAnimation = PlayerDeadAnimation.Normal;
			break;
		case DamageType.Kinetic:
			playerDeadAnimation = PlayerDeadAnimation.Normal;
			break;
		case DamageType.Non:
			playerDeadAnimation = PlayerDeadAnimation.Normal;
			break;
		case DamageType.Potion:
			playerDeadAnimation = PlayerDeadAnimation.Toxic;
			break;
		default:
			playerDeadAnimation = PlayerDeadAnimation.Normal;
			break;
		}
		if (isCriticalDamage && playerDeadAnimation == PlayerDeadAnimation.Normal)
		{
			playerDeadAnimation = PlayerDeadAnimation.HeadShot;
		}
		switch (playerDeadAnimation)
		{
		case PlayerDeadAnimation.Fire:
			timeForRespawn = 5.5f;
			if (base.isMine)
			{
				GameTopWindow.instance.deathMessagePanel.ChangeDurationBackground(5f);
			}
			break;
		case PlayerDeadAnimation.Freezing:
			timeForRespawn = 7f;
			if (base.isMine)
			{
				GameTopWindow.instance.deathMessagePanel.ChangeDurationBackground(6.5f);
			}
			break;
		case PlayerDeadAnimation.HeadShot:
			timeForRespawn = 2f;
			if (base.isMine)
			{
				GameTopWindow.instance.deathMessagePanel.ChangeDurationBackground(2f);
			}
			break;
		case PlayerDeadAnimation.Normal:
			timeForRespawn = 2f;
			if (base.isMine)
			{
				GameTopWindow.instance.deathMessagePanel.ChangeDurationBackground(2f);
			}
			break;
		case PlayerDeadAnimation.Toxic:
			timeForRespawn = 4.5f;
			if (base.isMine)
			{
				GameTopWindow.instance.deathMessagePanel.ChangeDurationBackground(4f);
			}
			break;
		}
		mecanimWrapper.Dead(playerDeadAnimation);
	}

	private new void OnDisable()
	{
		iconMiniMap.enabled = false;
	}
}
